package com.itheima.sh.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.sh.exception.ExceptionCast;
import com.itheima.sh.mapper.UserMapper;
import com.itheima.sh.pojo.User;
import com.itheima.sh.response.StatusCode;
import com.itheima.sh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public List<User> findAll() {
        //int i = 1 / 0;
        return userMapper.findAll();
    }

    public boolean add(User user) {
        // 参数校验
        if (user.getRealName() == null) {
            ExceptionCast.cast( StatusCode.USER_DATAPARAM_INVALID_ERROR );
        }
        return userMapper.add(user);
    }

    public boolean delete(Integer uuid){
        return userMapper.delete(uuid);
    }

    public boolean update(User user) {
        return userMapper.update(user);
    }

    @Override
    public Page<User> search(User user,Integer page, Integer size) {
        PageHelper.startPage(page, size);
        Page<User> pageList =
                (Page<User>) userMapper.search(user);
        return pageList;
    }
}
